package com.xcexample.adclasscard.login

import android.app.ActionBar.LayoutParams
import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import com.google.gson.GsonBuilder
import com.tencent.mmkv.MMKV
import com.xcexample.adclasscard.log.LogUtils
import com.xcexample.adclasscard.databinding.LayoutLogoutBinding
import com.xcexample.adclasscard.net.ApiFactory
import com.xcexample.adclasscard.net.BaseURL
import okhttp3.ResponseBody
import org.json.JSONException
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class LogoutFragment( var token: (data: String) -> Unit) : DialogFragment() {

    private var _binding: LayoutLogoutBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        _binding = LayoutLogoutBinding.inflate(inflater, container, false)
        return binding.root

    }
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        if (dialog.window != null) {
            dialog.window!!.setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }

        return dialog
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        _binding?.tvLogin?.setOnClickListener {
            //admin xckj@2019
            //17637502443 123456
            var account = binding.etAccount.text.toString().trim()
            var pass = binding.etPass.text.toString().trim()
            if (TextUtils.isEmpty(account)) {
                Toast.makeText(this.context, "请输入账号", Toast.LENGTH_SHORT).show()

                return@setOnClickListener
            }
            if (TextUtils.isEmpty(pass)) {
                Toast.makeText(this.context, "请输入密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            var retrofit = ApiFactory.create(BaseURL,false)
            var call = retrofit.logoutByPwd(account, pass)
            call!!.enqueue(object : Callback<ResponseBody?> {
                override fun onResponse(
                    call: Call<ResponseBody?>,
                    response: Response<ResponseBody?>
                ) {
                    val json = response.body()?.string()
                    try {
                        val gson = GsonBuilder().create()
                        val obj = gson.fromJson(json, LoginResponse::class.java)
                        LogUtils.e("result:$obj")
                        if (obj.code == 0) {
                            MMKV.defaultMMKV().clear()
                            token("")
                            dismiss()
                            activity?.finish()
                        } else {
                            Toast.makeText(this@LogoutFragment.context, obj.msg, Toast.LENGTH_SHORT)
                                .show()
                        }

                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }

                }

                override fun onFailure(call: Call<ResponseBody?>, t: Throwable) {

                }
            })


        }
        _binding?.tvCancel?.setOnClickListener {
            dismiss()
        }

    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onStart() {
        super.onStart()
        val displayMetrics = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
        val width = displayMetrics.widthPixels * 0.3
        dialog?.window?.setLayout(width.toInt(), LayoutParams.WRAP_CONTENT)
    }
}